package com.ewanghuiju.app.ui.resale.activity;

import android.os.Bundle;
import android.view.View;

import com.androidkun.xtablayout.XTabLayout;
import com.ewanghuiju.app.R;
import com.ewanghuiju.app.app.Constants;
import com.ewanghuiju.app.base.SimpleActivity;
import com.ewanghuiju.app.model.bean.local.TabEntity;
import com.ewanghuiju.app.ui.main.adapter.MyPagerAdapter;
import com.ewanghuiju.app.ui.my.activity.ShouyiCenterActivity;
import com.ewanghuiju.app.ui.resale.fragment.ResaleOrderListFragment;
import com.ewanghuiju.app.util.StartActivityUtil;
import com.ewanghuiju.app.util.nodoubleclick.AntiShake;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.SegmentTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.gyf.immersionbar.ImmersionBar;

import java.util.ArrayList;
import java.util.List;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author sj  qq:751943509
 * @class 转售订单列表
 * @time 2020/2/19
 */
public class ResaleOrderListActivity extends SimpleActivity {
    @BindView(R.id.viewpage)
    ViewPager viewpage;
    @BindView(R.id.commonTabLayout)
    CommonTabLayout commonTabLayout;
//    @BindView(R.id.xtabLayout)
//    XTabLayout xtabLayout;
    @BindView(R.id.segmentTabLayout)
    SegmentTabLayout segmentTabLayout;
//    @BindView(R.id.tv_title_right)
//    TextView tvTitleRight;

    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();

    private List<Fragment> fragmentList;
    private String[] mTitles = {"我发布的", "我买到的"};
    private String[] publish_list_Title = {"全部", "发布中", "交易成功"};
    private String[] buy_list_Title = {"全部", "待发货", "待收货","已完成"};

    private String[] online_list_Title = {"全部", "发布中", "交易成功"};

    private List<String> strList;

    @Override
    protected int getLayout() {
        return R.layout.activity_resale_orderlist;
    }

    @Override
    protected void initEventAndData() {
        segmentTabLayout.setTabData(mTitles);
        segmentTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                if(position==0){
                    online_list_Title = publish_list_Title;
                }else{
                    online_list_Title = buy_list_Title;
                }
                initFragment();
            }

            @Override
            public void onTabReselect(int position) {
            }
        });
        initFragment();
    }

    @Override
    protected int getTitleIntText() {
        return R.string.pintuan_order_title;
    }

    private void initFragment() {
        fragmentList = new ArrayList<>();
        strList = new ArrayList<>();
        mTabEntities.clear();
        for (int i = 0; i < online_list_Title.length; i++) {
            mTabEntities.add(new TabEntity(online_list_Title[i], R.mipmap.icon_selected_tab, 0));
            strList.add(online_list_Title[i]);
            ResaleOrderListFragment resaleOrderListFragment = new ResaleOrderListFragment();
            Bundle bundle = new Bundle();
            bundle.putInt(Constants.PINTUANORDER_TYPE, i);
            resaleOrderListFragment.setArguments(bundle);
            fragmentList.add(resaleOrderListFragment);
        }
        viewpage.setAdapter(new MyPagerAdapter(getSupportFragmentManager(), 0, fragmentList, strList));
        commonTabLayout.setTabData(mTabEntities);
        commonTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                viewpage.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {
            }
        });

        viewpage.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                commonTabLayout.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
//        int currentPage = getIntent().getIntExtra(Constants.PINTUANORDER_TYPE, 0);
        commonTabLayout.setCurrentTab(0);
        viewpage.setCurrentItem(0);
//
//        xtabLayout.setupWithViewPager(viewpage);
    }


//    @OnClick({R.id.tv_title_right})
//    public void doClick(View view) {
//        if (AntiShake.check(view.getId())) {    //判断是否多次点击
//            return;
//        }
//        switch (view.getId()) {
//            case R.id.tv_title_right:
//                //拼团收益
//                new StartActivityUtil(mContext, ShouyiCenterActivity.class).startActivity(true);
//                break;
//            default:
//                break;
//        }
//    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this)
                .titleBar(mToolbar, true)
                .statusBarDarkFont(true)
                .init();
    }

}
